/**
 * 
 */
package com.jeecms.wicool.action.shops;

import static com.jeecms.common.page.SimplePage.cpn;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.shops.ScoreShareSet;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.shops.ScoreShareSetMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

/**
 * @author spring
 *
 */
@Controller
public class ScoreShareSetAct {

	@Autowired
	private ScoreShareSetMng scoreShareSetManager;
	
	@Autowired
	private WiShopInfoMng shopMng;
	
	@RequestMapping("/scoreShareSet/v_list.do")
	public String list(Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = scoreShareSetManager.getPage(cpn(pageNo), CookieUtils.getPageSize(request),CmsUtils.getUser(request));
		model.addAttribute("pagination", pagination);
		return "shops/list_ScoreShareSet";
	}
	
	@RequestMapping("/scoreShareSet/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		ScoreShareSet scoreShareSet= new ScoreShareSet();
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("scoreShareSet", scoreShareSet);
		model.addAttribute("listShop", listShop);
		return "shops/add_ScoreShareSet";
	}
	
	@RequestMapping("/scoreShareSet/v_save.do")
	public String save(ScoreShareSet bean,Integer shopId, Integer pageNo,HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = scoreShareSetManager.save(bean,shopId);
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/scoreShareSet/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(id, errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ScoreShareSet entity = scoreShareSetManager.findById(id);
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("scoreShareSet", entity);
		model.addAttribute("listShop", listShop);
		return "shops/edit_ScoreShareSet";
	}
	
	@RequestMapping("/scoreShareSet/v_update.do")
	public String update(ScoreShareSet bean, Integer shopId,String loginName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(bean.getId(), errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = scoreShareSetManager.update(bean,shopId);
		model.addAttribute("message", "global.success");
		return list(pageNo, request, model);
	}
	
	@RequestMapping("/scoreShareSet/v_delete.do")
	public String delete(Integer[] ids, Integer pageNo,HttpServletRequest request, ModelMap model) {
		WebErrors errors =  WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		scoreShareSetManager.deleteByIds(ids);
		return list(pageNo, request, model);
	}
	
	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		ScoreShareSet entity = scoreShareSetManager.findById(id);
		if (errors.ifNotExist(entity, ScoreShareSet.class, id)) {
			return true;
		}
		return false;
	}
}
